package com.sm.ui;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sm.domain.Score;
import com.sm.service.IScoreService;
import com.sm.util.JTableUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

@Component
public class ScoreSelectUI implements BaseUI {

    @Autowired
    private IScoreService service;
    @Autowired
    private RedisTemplate redisTemplate;
    private JPanel panel;
    private JScrollPane scrollPane;
    private JTable table;
    private JTabbedPane tablePane;

    @Override
    public void init(JTabbedPane tablePane){
        this.tablePane = tablePane;
        panel = new JPanel();
        createSelect();
        createTitle();
        createList();
    }

    private void createTitle(){
        JPanel jb = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel lNo = new JLabel("以下是您的成绩：");
        lNo.setFont(new java.awt.Font("幼圆", 1, 24));
        jb.add(lNo);
        panel.add(jb);
    }

    private void createSelect(){
        JPanel jb = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel lNo = new JLabel("课程：");
        JTextField tNo = new JTextField(16);
        JButton btnAdd = new JButton("查询");
        jb.add(lNo);
        jb.add(tNo);
        jb.add(btnAdd);
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                DefaultTableModel dtm = (DefaultTableModel) table.getModel();
                Score vo = new Score();
                vo.setcName(tNo.getText());
                select(dtm, vo);
            }
        });
        panel.add(jb);
    }


    private void createList(){
        tablePane.addTab("成绩查询", panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        String[] columnNames = {"课程", "学期", "分数", "等级"};
        DefaultTableModel dataModel = new DefaultTableModel();
        dataModel.setColumnIdentifiers(columnNames);
        select(dataModel, null);
        table = new JTable(dataModel) {
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        JTableUtil.setTableColumnCenter(table);
        JTableUtil.setbgcolor(table);
        // 通过JTable对象创建JScrollPane，显示数据
        scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
    }

    private void select(DefaultTableModel dataModel, Score vo){
        dataModel.setRowCount(0);
        QueryWrapper<Score> qw = getQueryWrapper(vo);
        List<Score> list = service.listAll(qw);
        for (Score l : list) {
            Vector v = new Vector();
            v.add(l.getcName());
            v.add(l.getXq());
            v.add(l.getScore());
            v.add(l.getLevel());
            dataModel.addRow(v);
        }
    }

    private QueryWrapper<Score> getQueryWrapper(Score vo){
        QueryWrapper<Score> qw = new QueryWrapper();
        qw.eq("t.stuId", redisTemplate.opsForValue().get("studentId"));
        if (vo == null) {
            return qw;
        }
        if (StringUtils.isNotEmpty(vo.getcName())) {
            qw.eq("c.courseName", vo.getcName());
        }
        return qw;
    }


}

